package com.weichat.accessToken;

import java.io.IOException;

public class SingleAccessToken {
	
	private static final String appid="APPID";//你自己的appid
    private static final String appsecret="APPSECRET";//你自己的appsecret

    private AccessToken accessToken;
    private static SingleAccessToken singleAccessToken;
    /**
     * 私有构造函数
     * @throws IOException 
     */
    private SingleAccessToken() throws IOException{

        accessToken=GetAccessToken.getAccessToken(appid, appsecret);
        initThread();
    }
    /**
     * 获取SingleAccessToken对象
     * @return
     * @throws IOException 
     */
    public static SingleAccessToken getInstance() throws IOException{
        if(singleAccessToken==null){
            singleAccessToken=new SingleAccessToken();
        }
        return singleAccessToken;
    }

    public AccessToken getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(AccessToken accessToken) {
        this.accessToken = accessToken;
    }
    /**
     * 开启线程，设置SingleAccessToken为空
     */
    private void initThread(){
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                   //睡眠7000秒
                    Thread.sleep(7000*1000);    
                    singleAccessToken=null;

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }).start();
    }

}
